#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
class Solution {
public:
    vector<int> partitionLabels(string s) {
        int end[26] = { 0 };
        vector<int> ans;
        for (int i = 0; i < s.size(); ++i)
             end[s[i] - 'a'] = i;
        for (int left = 0, right = 0, n = end[s[0] - 'a']; right < s.size(); ++right) {
            if (right == n) {
                ans.emplace_back(n - left + 1);
                if (n + 1 < s.size()) {
                    left = n + 1;
                    n = end[s[left] - 'a'];
                }
            }
            else if (end[s[right] - 'a'] > n)
                n = end[s[right] - 'a'];
        }
        return ans;
    }
};